package net.net23.sysdent.client.ui.widgets.links;

import net.net23.sysdent.client.controller.AppController;
import net.net23.sysdent.client.services.LinksServiceAsync;
import net.net23.sysdent.client.ui.widgets.dialog.T4LDialog;
import net.net23.sysdent.client.ui.widgets.dialog.T4LDialogHandler;
import net.net23.sysdent.shared.jto.Link;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class T4LLinkItem extends Composite implements T4LDialogHandler {

	private Link link;
	private String tag;

	private DialogBox dialogBox;

	public T4LLinkItem(LinksServiceAsync reloadService, Link link) {
		this.link = link;
		initLinkItem(reloadService);
	}

	private void initLinkItem(final LinksServiceAsync reloadService) {
		FlexTable linkContainer = new FlexTable();
		linkContainer.setWidth("100%");
		FlexCellFormatter flexCellFormatter = linkContainer
				.getFlexCellFormatter();
		flexCellFormatter.setRowSpan(0, 0, 3);
		flexCellFormatter.setWidth(0, 0, "16px");
		flexCellFormatter.setVerticalAlignment(0, 0,
				HasVerticalAlignment.ALIGN_TOP);
		linkContainer.setWidget(0, 0,
				new Image(AppController.images.miniLink()));

		Anchor anchor = new Anchor(this.link.getName(), this.link.getLink(),
				"_blank");
		flexCellFormatter.setColSpan(0, 1, 3);
		linkContainer.setWidget(0, 1, anchor);

		flexCellFormatter.setColSpan(1, 0, 3);
		flexCellFormatter.setAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT,
				HasVerticalAlignment.ALIGN_TOP);
		linkContainer.setWidget(1, 0, new Label(link.getShortCreationDate()));

		Anchor edit = new Anchor("Editar");
		edit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				EditLinkForm editLinkForm = new EditLinkForm(T4LLinkItem.this,
						reloadService, link);
				dialogBox = T4LDialog.getDialogBox("Editar link",
						editLinkForm, false);
				dialogBox.show();
			}
		});
		flexCellFormatter.setWidth(2, 0, "35px");
		flexCellFormatter.setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		linkContainer.setWidget(2, 0, edit);
		Anchor navigate = new Anchor("Navegar");
		flexCellFormatter.setWidth(2, 1, "35px");
		flexCellFormatter.setHorizontalAlignment(2, 1,
				HasHorizontalAlignment.ALIGN_LEFT);
		linkContainer.setWidget(2, 1, navigate);

		flexCellFormatter.setHorizontalAlignment(2, 2,
				HasHorizontalAlignment.ALIGN_RIGHT);
		linkContainer.setWidget(2, 2, new Label(this.tag));

		sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);

		initWidget(linkContainer);

		addStyleName("t4l-link-item");
	}

	public void onBrowserEvent(Event event) {
		switch (DOM.eventGetType(event)) {
		case Event.ONMOUSEOVER: {
			removeStyleName("t4l-link-item");
			addStyleName("t4l-link-item-over");
			break;
		}
		case Event.ONMOUSEOUT: {
			removeStyleName("t4l-link-item-over");
			addStyleName("t4l-link-item");
			break;
		}
		}
	}

	@Override
	public void close() {
		if (dialogBox != null) {
			dialogBox.hide();
			dialogBox = null;
		}
	}
}
